﻿global using Microsoft.EntityFrameworkCore.Metadata.Builders;

global using System.ComponentModel.DataAnnotations.Schema;

using OpenPms.Models;

namespace OpenPms.Data;
/// <summary>
///  ii.RateId, ii.Date, ii.RoomTypeId
/// </summary>
[EntityTypeConfiguration(typeof(TPriceConfig))]
public class TPrice
{
    public Guid RateId { get; set; }
    [Display(Name = "有效期")]
    public DateOnly Date { get; set; }
    [DisplayName("房型")]
    public Guid RoomTypeId { get; set; }
    [DisplayName("周日")]
    public int P0 { get; set; }
    [DisplayName("周一")]
    public int P1 { get; set; }
    [DisplayName("周二")]
    public int P2 { get; set; }
    [DisplayName("周三")]
    public int P3 { get; set; }
    [DisplayName("周四")]
    public int P4 { get; set; }
    [DisplayName("周五")]
    public int P5 { get; set; }
    [DisplayName("周六")]
    public int P6 { get; set; }

    [DisplayName("周日")]
    public int? D0 { get; set; }
    [DisplayName("周一")]
    public int? D1 { get; set; }
    [DisplayName("周二")]
    public int? D2 { get; set; }
    [DisplayName("周三")]
    public int? D3 { get; set; }
    [DisplayName("周四")]
    public int? D4 { get; set; }
    [DisplayName("周五")]
    public int? D5 { get; set; }
    [DisplayName("周六")]
    public int? D6 { get; set; }
    [ForeignKey(nameof(RoomTypeId))]
    public TRoomType RoomType { get; set; } = default!;
    [ForeignKey(nameof(RateId))]
    public TRate Rate { get; set; } = default!;

    [DisplayName("天数")]
    [NotMapped]
    public int Days { get; set; }
    [NotMapped]
    [DisplayName("房型名称")]
    public string? RoomTypeName { get; set; }
    [NotMapped]
    [DisplayName("价格")]
    public int All { get; set; }
    /// <summary>
    /// 移动版路由参数
    /// </summary>
    [NotMapped]
    public int DayNumber { get => Date.DayNumber; }
    public void SetRoomType()
    {
        RoomTypeName = RoomType?.Name;
    }

    public static void SetDay(List<TPrice> items)
    {
        var date = DateOnly.FromDateTime(DateTime.Today);
        foreach (var item in items)
        {
            if (item.Date > date)
            {
                item.Days = item.Date.DayNumber - date.DayNumber;
                date = item.Date;
            }
        }
    }

    public List<int> GetPrices(List<int> ids)
    {
        List<int> items = new List<int>();
        foreach (var id in ids)
            items.Add(GetPrice(id));
        return items;
    }
    public int GetPrice(int weekIndex) => weekIndex switch
    {
        1 => P1,
        2 => P2,
        3 => P3,
        4 => P4,
        5 => P5,
        6 => P6,
        _ => P0
    };
    private static Type ThisType = typeof(TPrice);
    public VPrice GetPrice(DateOnly date)
    {
        var weekIdnex = (int)date.DayOfWeek;
        var p = ThisType.GetProperty("P" + weekIdnex);
        var d = ThisType.GetProperty("D" + weekIdnex);
        return new VPrice(date, (int)p!.GetValue(this)!, (int?)d!.GetValue(this));
        //switch (date.DayOfWeek)
        //{
        //    case DayOfWeek.Sunday:
        //        return new VPrice(date, P0, D0);
        //    case DayOfWeek.Monday:
        //        return new VPrice(date, P1, D1);
        //    case DayOfWeek.Tuesday:
        //        return new VPrice(date, P2, D2);
        //    case DayOfWeek.Wednesday:
        //        return new VPrice(date, P3, D3);
        //    case DayOfWeek.Thursday:
        //        return new VPrice(date, P4, D4);
        //    case DayOfWeek.Friday:
        //        return new VPrice(date, P5, D5);
        //    case DayOfWeek.Saturday:
        //        break;
        //    default:
        //        break;
        //}
        //GetPrice((int)date.DayOfWeek);
    }
    public void SetPrice(int price, List<int> weekIndexs)
    {
        var type = this.GetType();
        foreach (var weekIndex in weekIndexs)
            type.GetProperty("P" + weekIndex)!.SetValue(this, price);
    }












}

public class TPriceConfig : IEntityTypeConfiguration<TPrice>
{
    public void Configure(EntityTypeBuilder<TPrice> builder)
    {
        builder.HasKey(ii => new { ii.RateId, ii.Date, ii.RoomTypeId });
    }
}

//public class TPrice
//{
//    public int Id { get; set; }
//    [DisplayName("房价码")]
//    public int PriceRateId { get; set; }
//    [DisplayName("开始日期")]
//    [Column(TypeName = "date")]
//    public DateTime DateStart { get; set; }
//    [DisplayName("结束时间")]
//    [Column(TypeName = "date")]
//    public DateTime DateEnd { get; set; }
//    [DisplayName("房价模板")]
//    public int PriceTemplateId { get; set; }

//    //[ForeignKey(nameof(PlanKVId))]
//    //public TKV PlanKV { get; set; } = default!;
//    [ForeignKey(nameof(PriceTemplateId))]
//    [DisplayName("房价模板")]
//    public TKV PriceKV { get; set; } = default!;
//    [NotMapped]
//    [DisplayName("房价模板")]
//    public string PriceTemplateName { get => PriceKV?.Value ?? ""; }

//    [NotMapped]
//    [DisplayName("开始日期")]
//    public string DateStartString { get => DateStart.ToString("yyyy/MM/dd ddd"); }
//    [NotMapped]
//    [DisplayName("结束日期")]
//    public string DateEndString { get => DateEnd.ToString("yyyy/MM/dd ddd"); }
//    [NotMapped]
//    public string Create { get => "添加"; }
//    [NotMapped]
//    public int DateStartDayNumber { get=>DateOnly.FromDateTime(DateStart).DayNumber; }
//    public int DateEndDayNumber { get=>DateOnly.FromDateTime(DateEnd).DayNumber; }
//}

